package com.haredot.vo;

import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * LayUi 分页结果
 * {
 * "code": 0,
 * "msg": "",
 * "count": 1000,
 * "data": [{}, {}]
 * }
 */
public final class LayuiPageResultVo<T> implements Serializable {
    /**
     * 状态码， 0 代表成功
     */
    private Integer code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 总条数
     */
    private Long count;
    /**
     * 每页显示的内容
     */
    private List<T> data;
    /**
     * 合并行
     */
    private Map<String, ?> totalRow ;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Map<String, ?> getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(Map<String, ?> totalRow) {
        this.totalRow = totalRow;
    }

    /**
     * 将 R 转换为 LayUi的结果
     * @param result
     * @param <T>
     * @return
     */
    public static <T> LayuiPageResultVo<T> convertR(R<Page<T>> result) {
        LayuiPageResultVo<T> objectLayuiPageResultVo = new LayuiPageResultVo<>();
        objectLayuiPageResultVo.setCode(result.getStatus() ? 0 : result.getCode());
        objectLayuiPageResultVo.setMsg(result.getMessage());

        Page<T> page = result.getData();
        objectLayuiPageResultVo.setCount(page.getTotal());
        objectLayuiPageResultVo.setData(page.getResult());
        return objectLayuiPageResultVo;
    }
}
